package com.wang.list;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * List的特点
 * 有下标,可以重复
 */
public class ListDemo1 {
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add("a");
        list.add(1,"b");
        list.add(2,"c");
        System.out.println(list.toString());
        Object a1 = list.set(0, "a1");
        System.out.println(a1);
        System.out.println(list.size());
        System.out.println(list.toString());
        //删除
        list.remove(0);
        System.out.println(list.size());
        System.out.println(list.toString());
        //foreach遍历
        for (Object s:list) {
            System.out.println(s);
        }
        //for下标遍历
        //...

        //iterator迭代器遍历
        Iterator iterator = list.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
        //ListIterator 这个是list特有的list迭代器
        //这个功能要比iterator强,支持任意方向迭代,且可以添加删除修改元素
        /**
         * 1.hasNext() 2.hasPrevious() 3.next() 4.previous()
         * 5.nextIndex(),6previousIndex() 7.remove()
         * 8.add() 9.set()
         */
        //正向
        ListIterator listIterator = list.listIterator();
        while (listIterator.hasNext()){
            System.out.println(listIterator.nextIndex()+""+listIterator.next());
        }
        //逆向
        while (listIterator.hasPrevious()){
            System.out.println(listIterator.previousIndex()+""+listIterator.previous());
        }
        //判断
        if (list.contains("a"))System.out.println("has a");
        System.out.println(list.indexOf("a"));
    }
}
